var app = getApp();
Page({
  data:{
    resourceUrl3: app.globalData.apiUrl + '/public/uploads/goods/',
    list:'',
    banner_img:'',
    resourceUrl:app.globalData.apiUrl+'/public/uploads/banner/',
    resourceUrl2: app.globalData.apiUrl + '/public/uploads/nav/',
    guanggao:["/../image/4.jpg"],
    coupons: [
          { "moneyMax": "20", "name": "今日优惠", "moneyHreshold": "400" },
          { "moneyMax": "10", "name": "今日优惠", "moneyHreshold": "300" },
          { "moneyMax": "5", "name": "今日优惠", "moneyHreshold": "200" }] ,
    indicatorDots: false,
    autoplay: false,
    interval: 3000,
    duration: 800
  },
  onLoad:function(options){
    var that = this;
    var openid = decodeURIComponent(options.openid)//二维码携带的信息
    var leader = decodeURIComponent(options.scene)
    if(leader != 'undefined'){
          wx.redirectTo({
            url: '/pages/leader/leader?leader='+leader
          })
          console.log('二维码推广进入')
          
    }
  
    // bannerImage
    wx.request({
      url: app.globalData.apiUrl+'/api/banner/get_banner',
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        
        // console.log(res);
        that.setData({banner_img:res.data})
      },
      fail: function(res) {},
      complete: function(res) {},
    })
    // nav
    wx.request({
      url: app.globalData.apiUrl + '/api/nav/navList',
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res.data)
        that.setData({ navList: res.data })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    //coupon
    wx.request({
      url: app.globalData.apiUrl + '/api/coupon/getCoupon',
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        console.log(res)
        that.setData({coupons:res.data})
      },
      fail: function(res) {},
      complete: function(res) {},
    })
    wx.request({
      url: app.globalData.apiUrl + '/api/goods/get_goods',
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
        that.setData({ list: res.data })
      },
      fail: function (res) { },
      complete: function (res) { },
    })

  },
  onReady:function () {
    
  },
  gitCoupon:function(res){
    var open_id = app.globalData.userInfo.open_id
    if(open_id){
      //领取优惠券
      wx.request({
        url: app.globalData.apiUrl + '/api/user/getCoupon',
        data: { openid: open_id, id: res.currentTarget.dataset.id},
        method: 'GET',
        dataType: 'json',
        success: function(res) {
          console.log(res)
        },
        fail: function(res) {},
        complete: function(res) {},
      })
      wx.showToast({
        title: '领取成功',
        icon: 'success'
      })
    }else{
      wx.showToast({
        title: '领取失败,请登入后领取',
        icon: 'none'
      })
    }
  }
})